//
//  RegisterVC.m
//  Doctor
//
//  Created by 阳光 on 16/9/10.
//  Copyright © 2016年 阳光. All rights reserved.
//  注册界面

#import "RegisterVC.h"
#import "LoginVC.h"
#import "ConfirmVC.h"

@interface RegisterVC ()<CustomNavViewDelegate>

@end



@implementation RegisterVC

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBarHidden = NO;
}




-(void)rightBtnEvent
{
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    self.view.backgroundColor=WhiteColor;
    
    [self initMainView];
    
}

-(void)leftBtnEvent
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

-(void)initMainView
{
    CustomNavView *registerNavView=[[CustomNavView alloc]initWithTitle:NSLocalizedString(@"Sign Up", nil) leftBtnImage:@"back_gray" hasLeftBtnImage:YES rightBtnTitle:nil hasRightBtnTitle:NO];//@"注册"
    registerNavView.delegate=self;
    registerNavView.frame=CGRectMake(0,0,APPW,64);
    [self.view addSubview:registerNavView];
    
    // 用户名
    LeftTitle_TextFieldView *userNameView=[[LeftTitle_TextFieldView alloc]initWithTitle:NSLocalizedString(@"Username", nil) placeholder:NSLocalizedString(@"EnterName", nil)];//@"用户名";@"请输入用户名"
    ViewBorderRadius(userNameView,kmargin/2,.5,ColorBDBDBD);
    [self.view addSubview:userNameView];
    userNameView.tag=100;
    [userNameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(100);
        make.left.mas_equalTo(kmargin*3);
        make.size.mas_equalTo(CGSizeMake(APPW-kmargin*6,40));
    }];
    
    
    
    // 密码
    LeftTitle_TextFieldView *passwordView=[[LeftTitle_TextFieldView alloc]initWithTitle:NSLocalizedString(@"Password", nil) placeholder:NSLocalizedString(@"EnterPassword", nil)];//@"密码";@"请输入密码"
    ViewBorderRadius(passwordView,kmargin/2,.5,ColorBDBDBD);
    [self.view addSubview:passwordView];
    passwordView.tag=101;
    passwordView.textField.secureTextEntry=YES;
    [passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(userNameView.mas_bottom).offset(kmargin);
        make.left.mas_equalTo(kmargin*3);
        make.size.mas_equalTo(CGSizeMake(APPW-kmargin*6,40));
    }];
    
    // 确认密码
    LeftTitle_TextFieldView *againPdView=[[LeftTitle_TextFieldView alloc]initWithTitle:NSLocalizedString(@"SurePassword", nil) placeholder:NSLocalizedString(@"EnterSurePassword", nil)];//@"确认密码";@"请再次输入密码"
    ViewBorderRadius(againPdView,kmargin/2,.5,ColorBDBDBD);
    [self.view addSubview:againPdView];
    againPdView.tag=102;
    againPdView.textField.secureTextEntry=YES;
    [againPdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(passwordView.mas_bottom).offset(kmargin);
        make.left.mas_equalTo(kmargin*3);
        make.size.mas_equalTo(CGSizeMake(APPW-kmargin*6,40));
    }];
   
    // 下一步
    UIButton *nextBtn=[UIButton new];
    [self.view addSubview:nextBtn];
    nextBtn.backgroundColor=Main_Color;
    nextBtn.titleLabel.font=Font_Number(16);
    ViewBorderRadius(nextBtn,kmargin/2,1,ClearColor);
    [nextBtn setTitle:NSLocalizedString(@"Next Step", nil) forState:UIControlStateNormal];
    [nextBtn setTitleColor:WhiteColor forState:UIControlStateNormal];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(againPdView.mas_bottom).offset(kmargin*2);
        make.size.mas_equalTo(CGSizeMake(APPW-kmargin*6,40));
        make.left.mas_equalTo(kmargin*3);
    }];
    [nextBtn addTarget:self action:@selector(nextBtnEvent) forControlEvents:UIControlEventTouchUpInside];
}

-(void)nextBtnEvent
{
    
    LeftTitle_TextFieldView *userNameView=(LeftTitle_TextFieldView *)[self.view viewWithTag:100];
    LeftTitle_TextFieldView *passwordView=(LeftTitle_TextFieldView *)[self.view viewWithTag:101];
    LeftTitle_TextFieldView *againPdView=(LeftTitle_TextFieldView *)[self.view viewWithTag:102];
    
    if (userNameView.textField.text.length==0)
    {
        [self.view makeToast:NSLocalizedString(@"Username cannot be empty", nil) duration:1.0 position:CSToastPositionCenter];
    }
    else  if (userNameView.textField.text.length<6||userNameView.textField.text.length>16)
    {
        [self.view makeToast:NSLocalizedString(@"Name limit", nil) duration:1.0 position:CSToastPositionCenter];
    }
    else  if (passwordView.textField.text.length==0)
    {
        [self.view makeToast:NSLocalizedString(@"Password cannot be empty", nil) duration:1.0 position:CSToastPositionCenter];
    }
    else  if (passwordView.textField.text.length!=8)
    {
        [self.view makeToast:NSLocalizedString(@"Please input 8 chars password", nil) duration:1.0 position:CSToastPositionCenter];
    }
    else  if (againPdView.textField.text.length==0)
    {
        [self.view makeToast:NSLocalizedString(@"Sure password can not be empty", nil) duration:1.0 position:CSToastPositionCenter];
    }
    else  if (againPdView.textField.text.length!=8)
    {
        [self.view makeToast:NSLocalizedString(@"Please input 8 chars sure password", nil) duration:1.0 position:CSToastPositionCenter];
   
    } else if (![againPdView.textField.text isEqualToString:passwordView.textField.text]){//...

        [self.view makeToast:NSLocalizedString(@"Password does not match", nil) duration:1.0 position:CSToastPositionCenter];
    }
    else
    {
        NSDictionary *dic=@{
                            @"username":userNameView.textField.text,
                            @"password":passwordView.textField.text,
                            @"repassword":againPdView.textField.text
                            };
        
        __weak typeof(self) weakSelf=self;
        [QBRequest RegisterWithDic:dic Success:^(id res)
         {
             
           [self.view makeToast:NSLocalizedString(@"Sign Up Success", nil) duration:1.0 position:CSToastPositionCenter];//res[@"message"]
             
             dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                 
                 if ([weakSelf.delegate respondsToSelector:@selector(registerToLoginWithUserName:password:)])
                 {
                     [weakSelf.delegate registerToLoginWithUserName:userNameView.textField.text password:passwordView.textField.text];
                 }
                 
                 [weakSelf dismissViewControllerAnimated:NO completion:nil];
                 
             });
             
         } failure:^(NSString *error)
         {
             
         }];
    }
}


@end


